/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practiceexamples;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author rdisalvo
 */
public class UpdateGUIFrame extends JFrame {
//just trying to figure out the goddamn mercurial crap.

//    "Once a Swing component has been realized, all code that
//    might affect or depend on the state of that component
//    should be executed in the event-dispatching thread"

// This is because calls to and from the GUI should be consistently synchronized.
    // luckily, we can slow down our updating to the gui
    // by creating a thread that creates threads to be queued up for
    // processing by Swing.

    // ANYTHING THAT PUTS DATA INTO THE GUI FRAME
    // OUGHT TO BE DONE VIA AN ITEM ON THE EVENTQUEUE.
    // ANYTHING THAT TAKES DATA FROM THE GUI FRAME
    // OUGHT TO BE DONE VIA AN ITEM ON THE EVENTQUEUE ALSO.

    // this makes things just a little bit tricky...

    JLabel counter = new JLabel("");
    JTextField field = new javax.swing.JTextField(20);
    String fieldText;
    JLabel info = new JLabel("Divide Results by A Quantity by putting it Above");
    String infoText;
    int divideBy = 1;
    int secondCounter = 0;
    int millisecCounter = 0;
    int secondCDivided = 0;
    int millisecCDivided = 0;
    int stepsTaken = 0;

    public UpdateGUIFrame(String s) {
        super(s);
        this.setSize(400, 100);
        this.setLayout(new java.awt.FlowLayout());
        this.add(counter);
        this.add(field);
        this.add(info);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setBackground(Color.WHITE);
        this.setResizable(false);
        
        FreezeFrame.centerJFrame(this);

        // NOW REALIZE THE FRAME!
        this.setVisible(true);
    }

    public static void main(String[] args){
        UpdateGUIFrame ugf = new UpdateGUIFrame("Figuring Out how to Run GUI and Game");
        Thread game = new Thread(new GameRunThread(ugf));
        Thread gui = new Thread(new GUIUpdateThread(ugf));
        game.start();
        gui.start();
        
    }

    public void getGUIInfo(){
        javax.swing.SwingUtilities.invokeLater(new Runnable(){

            public void run() {
                if(!field.getText().equals(""))
                fieldText = field.getText();
                else
                    fieldText=null;
            }

        });
    }

    // called once per ms
    public void stepVar() {
        if(fieldText==null){
            infoText = "Divide Results by A Quantity by putting it Above";
            divideBy = 1;
        }
        if(fieldText!=null)
            try{
                divideBy = Integer.parseInt(fieldText);
                infoText = "Divide Results by A Quantity by putting it Above";
            }catch(NumberFormatException e){
                infoText = "That is not a valid integer!";
            }
       
        millisecCounter++;

        stepsTaken++;
        if (stepsTaken % 1000 == 0) {
            // count will rise each second
            secondCounter++;
        }
        if (stepsTaken == 10000)
            stepsTaken = 0;
        secondCDivided = secondCounter / divideBy;
        millisecCDivided = millisecCounter / divideBy;
    }

    // called whenever the GUIUpdateThread decides to do so
    public void updateDisplay() {
        counter.setText("Sec Count: " + secondCDivided + "       Milli Count: " + millisecCDivided);
        if(infoText!=null){
            this.info.setText(infoText);
            infoText = null;
        }
    }

    static class GUIUpdateThread implements Runnable {

        UpdateGUIFrame gui;

        public GUIUpdateThread(UpdateGUIFrame gui) {
            this.gui = gui;
        }
        public void run() {
            while (true) {
                try {
                    // 100 ms heartbeat for updating the GUI
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdateGUIFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                // update the display
                // by spinning off a new updating thread
                // and putting it on the swing eventqueue
                javax.swing.SwingUtilities.invokeLater(new Runnable(){
                    public void run(){
                        gui.getGUIInfo();
                        gui.updateDisplay();
                    }
                });
            }

        }
    }

    static class GameRunThread implements Runnable {
        UpdateGUIFrame gui;


        public GameRunThread(UpdateGUIFrame gui) {
            this.gui = gui;
        }

        public void run() {
            while (true) {
                try {
                    // one-ms heartbeat for game running
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdateGUIFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                // step forward
                gui.stepVar();

            }

        }
    }
}
